<?php
class Articles extends MY_Controller {
    
    function __construct() {
        parent::__construct();
    }
    
    /**
     * 
     */
    public function index() {
    }

    /**
     * 
     */
    public function get($id = NULL) {
        $article = new Article();
        $article->where('id', $id)->include_related('category')->include_related('user')->get();
        $data['template'] = 'article';
        if ($article->exists()) {
            $data['article'] = $article;
            $this->load->view('template', $data);
        } else {
            redirect('errors/notFound');
        }
    }
    
    /**
     * 
     */
    public function create() {
        $article = new Article();
        if ($this->input->post('title')) {
            $article->title = $this->input->post('title');
        }
        if ($this->input->post('content')) { 
            $article->content = $this->input->post('content');
        }
        if ($article->save()) {
            echo 'success';
        } else {
            foreach ($article->error->all as $e) {
                echo $e . "<br />";
            }
        }

    }
    
     
    
    /**
     * 
     */
    public function update($id = NULL) {
        $article = new Article();
        $article->where('id', $id)->get();
        if ($article->exists()) {
            if (isset($_GET['title']) && ($_GET['title'] != '')) { 
                $article->title = $_GET['title'];
            }
            if (isset($_GET['content']) && ($_GET['content'] != '')) { 
                $article->content = $_GET['content'];
            }
            $article->save();
        } else {
            echo 'Article does not exist';
        }
    }
    
    /**
     * 
     */
    public function delete($id = NULL) {
        $article = new Article();
        $article->where('id', $id)->get();
        if ($article->exists()) {
            $article->delete();
        } else {
            echo 'Article does not exist';
        }
    }
    
}

?>